﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using BrainTechLLC;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace CompareMultiple.Plugin
{
    public partial class CompareMultipleImagesWindow : Window
    {
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResizeDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResize;
        public event EventHandler<ObjectEventArgsNonRef<RotationParams, bool>> RequestRotate;
        public event EventHandler<ObjectEventArgs<ObservableCollection<FileImageViewModel>>> ClosedWindow;
        public event EventHandler<ObjectEventArgs<FileImageViewModel>> WantViewFull;

        public static readonly DependencyProperty FileImageViewModelsProperty =
           DependencyProperty.Register("FileImageViewModels", typeof(ObservableCollection<FileImageViewModel>), typeof(CompareMultipleImagesWindow), new PropertyMetadata(null, OnFileImageViewModel1PropertyChanged));

        public ObservableCollection<FileImageViewModel> FileImageViewModels
        {
            get { return (ObservableCollection<FileImageViewModel>)GetValue(FileImageViewModelsProperty); }
            set { SetValue(FileImageViewModelsProperty, value); }
        }

        private static void OnFileImageViewModel1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CompareMultipleImagesWindow source = d as CompareMultipleImagesWindow;
            ObservableCollection<FileImageViewModel> v = e.NewValue as ObservableCollection<FileImageViewModel>;
            ObservableCollection<FileImageViewModel> vOld = e.OldValue as ObservableCollection<FileImageViewModel>;

            if (v != vOld)
            {
                source.UpdateFileImageViewModel1(v, vOld);
            }
        }

        private void UpdateFileImageViewModel1(ObservableCollection<FileImageViewModel> viewModel, ObservableCollection<FileImageViewModel> old)
        {
            compare.FileImageViewModels = viewModel;
        }

        public CompareMultipleImagesWindow()
        {
            InitializeComponent();
            Closing += new System.ComponentModel.CancelEventHandler(CompareImagesWindow_Closing);
        }

        void CompareImagesWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Closing -= CompareImagesWindow_Closing;
            
            FileImageViewModels.Clear();
            FileImageViewModels = null;

            if (Window1.MainWindow._AllFileImageCollectionViewModels != null)
            {
                Window1.MainWindow._AllFileImageCollectionViewModels.ForEach(o => o.Stats.Invalidate());
            }

            if (ClosedWindow != null)
                ClosedWindow(this, new ObjectEventArgs<ObservableCollection<FileImageViewModel>>(FileImageViewModels));
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                e.Handled = true;
                this.Close();
            }
        }

        private void compare_WantViewFull(object sender, ObjectEventArgs<FileImageViewModel> e)
        {
            if (WantViewFull != null)
                WantViewFull(this, e);
        }

        private void WantCompareMultipleInNewWindow(object sender, ExecutedRoutedEventArgs e)
        {
            ObservableCollection<FileImageViewModel> toCompare = e.Parameter as ObservableCollection<FileImageViewModel>;
            CompareMultipleWindowHandler.Handler.Handler.OpenAskWindow(toCompare, string.Empty);
        }

        private void compare_RequestDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            RequestDelete.RaiseEvent(sender, e.obj1, e.obj2);
        }

        private void compare_RequestResize(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            RequestResize.RaiseEvent(sender, e.obj1, e.obj2);
        }

        private void compare_RequestResizeDelete(object sender, ObjectEventArgsNonRef<FileImageViewModel, bool> e)
        {
            RequestResizeDelete.RaiseEvent(sender, e.obj1, e.obj2);
        }

        private void compare_RequestRotate(object sender, ObjectEventArgsNonRef<RotationParams, bool> e)
        {
            RequestRotate.RaiseEvent(sender, e.obj1, e.obj2);
        }
    }
}
